package GUI;


import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import client.ClientConsole;

import Library.ListOrderBookStudent;
import Library.ReaderCard;

/**
 * 
 */

/**
 * @author mishel hen
 *
 */
/**
 * This class is the main menu to student 
 */
public class GUI_MainMenuStudent extends JPanel
{
	/**
	 * panel for Search
	 */
	private GUI_Search s;
	/**
	 * panel for user loans
	 */
	private GUI_userLoans ul;
	/**
	 * panel for order book
	 */
	private GUI_OrderBook ob;
	/**
	 * panel for display reader card
	 */
	private GUI_DisplayReaderCard d;
	/**
	 * panel for loans student
	 */
	private GUI_LoanStudent ls;
	/**
	 * Checks for a reader card
	 */
	private int flag;
	GUI_MainMenuStudent mms = this;
	/**
	 * client Connected
	 */
	private ClientConsole clc;


	
	/**
	 * @param cc 
	 * @param lg
	 * @param msgNum  
	 */
	public GUI_MainMenuStudent(ClientConsole cc,final GUI_LogIn lg, int msgNum )
	{
		super();	
		this.setSize(600, 400);
		clc = cc;
		flag = msgNum;
		JLabel lblUsername = new JLabel("Hello " + clc.userName );
		lblUsername.setFont(new Font("Tahoma", Font.PLAIN, 24));

		lblUsername.setBounds(80, 20, 200, 20);
		add(lblUsername);
		
		
		JButton btnSearch = new JButton("Search");
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				s=new GUI_Search(clc,mms);
				lg.setContentPane(s);
				
		}});
	setLayout(null);
	btnSearch.setBounds(80, 80, 160, 23);
	this.add(btnSearch);
	
	
	   JButton btnExtend  = new JButton("Extend loan time");
	   btnExtend.addActionListener(new ActionListener() {
		   public void actionPerformed(ActionEvent arg0) {
			 String[] s={"getReaderId",clc.userName};
			 try {
				clc.client.sendToServer(s);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			 while(clc.client.flag==0)
				 System.out.print("");
			 
			 clc.client.flag=0;
			 
			 String readerId=(String) clc.client.message;
			 ul=new GUI_userLoans(clc,lg,mms,readerId);  
			 lg.setContentPane(ul);
			 
    	}});
   setLayout(null);
   btnExtend.setBounds(80, 140, 160, 23);
   this.add(btnExtend);
   
   
	   JButton btnOrderBook  = new JButton("Orders");
	   btnOrderBook.addActionListener(new ActionListener() {
		   public void actionPerformed(ActionEvent arg0) {
			   String s[]={"getListOrderBook",clc.userName};
				try {
					clc.client.sendToServer(s);
					while(clc.client.flag==0)
					    System.out.print("");
					    clc.client.flag=0;
					    ListOrderBookStudent listOrderBookStudent =(ListOrderBookStudent)clc.client.message;
					    if(listOrderBookStudent.ListOrderBookStudent.isEmpty())
					    {
					    	GUI_OrderBook ob= new GUI_OrderBook(clc,mms);
					    	clc.cgui.setContentPane(ob);
					    }
					    else
					    {
					    	GUI_ShowOrderBook sob= new GUI_ShowOrderBook(clc,listOrderBookStudent.ListOrderBookStudent,mms);
					    	clc.cgui.setContentPane(sob);
					    }
					    	
				} catch (IOException e) {
					e.printStackTrace();
				}	// send message to server
			 
		}});
	setLayout(null);
	btnOrderBook.setBounds(80, 200, 160, 23);
	this.add(btnOrderBook);
	
	JButton btnEdit = new JButton("Reader card");
	btnEdit.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent arg0) {
			String s[]={"Reader card",clc.userName};
			try {
				clc.client.sendToServer(s);
				while(clc.client.flag==0)
				    System.out.print("");
				    clc.client.flag=0;
			} catch (IOException e) {
				e.printStackTrace();
			}	// send message to server

			
			ReaderCard readercard =(ReaderCard)clc.client.message;
			d=new GUI_DisplayReaderCard(clc,readercard,mms);
			clc.cgui.setContentPane(d);
			
	}});
setLayout(null);
btnEdit.setBounds(80, 260, 160, 23);
this.add(btnEdit);

	      JButton btnHistory = new JButton("Loans ");
	      btnHistory.addActionListener(new ActionListener() {
		   public void actionPerformed(ActionEvent arg0) {
		 	    ls=new GUI_LoanStudent(clc,mms);
			 	clc.cgui.setContentPane(ls);

			   
			
	    }});
    setLayout(null);
    btnHistory.setBounds(80, 320, 160, 23);
    this.add(btnHistory);
    
    	JButton btnLogOut = new JButton("Log Out");
	btnLogOut.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			clc.cgui.LogOut();
		}
	});
	btnLogOut.setBounds(445, 320, 100, 23);
	add(btnLogOut);
	
		
	if (flag==0)
	{
	   btnExtend.setEnabled(false);
	   btnOrderBook.setEnabled(false);
	   btnEdit.setEnabled(false);
	   btnHistory.setEnabled(false);
	}
}
	
}







